package com.ruoyi.web.controller.system;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.MessageInfoUpdateStatusDto;
import com.ruoyi.system.domain.MessageInfo;
import com.ruoyi.system.domain.MessageInfoAddDto;
import com.ruoyi.system.domain.MessageInfoListDto;
import com.ruoyi.system.domain.MessageInfoUpdateDto;
import com.ruoyi.system.service.IMessageInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息Controller
 *
 * @author 杨杰
 * @date 2024-03-04
 */
//@Api(tags = {"消息推送管理"})
@Controller
@RequestMapping("/system/messageInfo")
public class MessageInfoController extends BaseController {
    private String prefix = "system/messageInfo";

    @Autowired
    private IMessageInfoService messageInfoService;

    @GetMapping()
    public String messageInfo() {
        return prefix + "/messageInfo";
    }


    /**
     * 导出消息列表
     */
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MessageInfo messageInfo) {
        List<MessageInfo> list = messageInfoService.selectMessageInfoList(messageInfo);
        ExcelUtil<MessageInfo> util = new ExcelUtil<MessageInfo>(MessageInfo.class);
        return util.exportExcel(list, "消息数据");
    }

    /**
     * 新增消息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


    /**
     * 查询消息列表
     */
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation(value = "查询消息列表", response = MessageInfo.class)

    public TableDataInfo list(MessageInfoListDto listDto) {
        startPage();
        MessageInfo messageInfo = new MessageInfo();
        BeanUtil.copyProperties(listDto, messageInfo);
        List<MessageInfo> list = messageInfoService.selectMessageInfoList(messageInfo);
        return getDataTable(list);
    }

    /**
     * 新增保存消息
     */
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "新增保存消息")

    public AjaxResult addSave(MessageInfoAddDto addDto) {
        MessageInfo messageInfo = new MessageInfo();
        BeanUtil.copyProperties(addDto, messageInfo);
        return toAjax(messageInfoService.insertMessageInfo(messageInfo));
    }

    /**
     * 修改消息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        MessageInfo messageInfo = messageInfoService.selectMessageInfoById(id);
        mmap.put("messageInfo", messageInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息
     */
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "修改保存消息")
    public AjaxResult editSave(MessageInfoUpdateDto updateDto) {
        MessageInfo messageInfo = new MessageInfo();
        BeanUtil.copyProperties(updateDto, messageInfo);
        return toAjax(messageInfoService.updateMessageInfo(messageInfo));
    }

    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PostMapping("/editStatus")
    @ResponseBody
    @ApiOperation(value = "全部已读")
    public AjaxResult editStatus(MessageInfoUpdateStatusDto updateDto) {
        List<Long> ids = updateDto.getIds();
        for (int i = 0; i < ids.size(); i++) {
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setId(ids.get(i));
            messageInfo.setState(updateDto.getState());
            messageInfoService.updateMessageInfo(messageInfo);
        }
//                      暂定
        return success();
    }


    /**
     * 删除消息
     */
    @Log(title = "消息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    @ApiOperation(value = "删除消息")
    public AjaxResult remove(String ids) {
        return toAjax(messageInfoService.deleteMessageInfoByIds(ids));
    }
}
